package com.example.acharten;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
	private HistogramChart hc;
	private CurveChart cc;
	private PieChart pc;
	private List<Map<String, Object>> ls;
	private ListView lv;
	private List<Map<String, Map<String, Object>>> data1;
	private Map<Object, Object> pieData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		String[] charts = new String[] { "直方图", "曲线图", "饼状图" };

		hc = new HistogramChart(); // 直方图类
		cc = new CurveChart();// 曲线图类
		pc = new PieChart();// 饼状图类
		/**
		 * 直方图测试数据
		 */
		List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
		ls = new ArrayList<Map<String, Object>>();

		Map<String, Object> mapNj = new HashMap<String, Object>();
		mapNj.put("南京", 100);

		Map<String, Object> mapNt = new HashMap<String, Object>();
		mapNt.put("南通", 80);

		Map<String, Object> mapWx = new HashMap<String, Object>();
		mapWx.put("无锡", 60);

		Map<String, Object> mapZj = new HashMap<String, Object>();
		mapZj.put("镇江", 70);

		Map<String, Object> mapSz = new HashMap<String, Object>();
		mapSz.put("苏州", 60);

		Map<String, Object> mapCz = new HashMap<String, Object>();
		mapCz.put("常州", 50);

		ls.add(mapNj);
		ls.add(mapNt);
		ls.add(mapWx);
		ls.add(mapZj);
		ls.add(mapSz);
		ls.add(mapCz);

		/**
		 * 曲线图测试数据
		 */
		Map<String, Object> scoreNj = new HashMap<String, Object>();
		scoreNj.put("1", 60);
		scoreNj.put("2", 45);
		scoreNj.put("3", 25);
		scoreNj.put("4", 75);
		scoreNj.put("5", 87);
		scoreNj.put("6", 52);
		scoreNj.put("7", 76);
		scoreNj.put("8", 64);
		scoreNj.put("9", 52);
		scoreNj.put("10", 75);
		scoreNj.put("11", 67);
		scoreNj.put("12", 54);

		Map<String, Object> scoreNt = new HashMap<String, Object>();
		scoreNt.put("1", 65);
		scoreNt.put("2", 52);
		scoreNt.put("3", 75);
		scoreNt.put("4", 62);
		scoreNt.put("5", 24);
		scoreNt.put("6", 75);
		scoreNt.put("7", 70);
		scoreNt.put("8", 62);
		scoreNt.put("9", 78);
		scoreNt.put("10", 26);
		scoreNt.put("11", 76);
		scoreNt.put("12", 54);

		Map<String, Object> scoreWx = new HashMap<String, Object>();
		scoreWx.put("1", 96);
		scoreWx.put("2", 23);
		scoreWx.put("3", 65);
		scoreWx.put("4", 53);
		scoreWx.put("5", 87);
		scoreWx.put("6", 76);
		scoreWx.put("7", 45);
		scoreWx.put("8", 54);
		scoreWx.put("9", 86);
		scoreWx.put("10", 25);
		scoreWx.put("11", 64);
		scoreWx.put("12", 54);

		Map<String, Object> scoreZj = new HashMap<String, Object>();
		scoreZj.put("1", 90);
		scoreZj.put("2", 10);
		scoreZj.put("3", 50);
		scoreZj.put("4", 35);
		scoreZj.put("5", 87);
		scoreZj.put("6", 49);
		scoreZj.put("7", 39);
		scoreZj.put("8", 59);
		scoreZj.put("9", 69);
		scoreZj.put("10", 37);
		scoreZj.put("11", 38);
		scoreZj.put("12", 19);

		Map<String, Object> scoreSz = new HashMap<String, Object>();
		scoreSz.put("1", 60);
		scoreSz.put("2", 40);
		scoreSz.put("3", 30);
		scoreSz.put("4", 70);
		scoreSz.put("5", 50);
		scoreSz.put("6", 10);
		scoreSz.put("7", 20);
		scoreSz.put("8", 30);
		scoreSz.put("9", 60);
		scoreSz.put("10", 80);
		scoreSz.put("11", 90);
		scoreSz.put("12", 10);

		Map<String, Object> scoreCz = new HashMap<String, Object>();
		scoreCz.put("1", 90);
		scoreCz.put("2", 60);
		scoreCz.put("3", 50);
		scoreCz.put("4", 80);
		scoreCz.put("5", 10);
		scoreCz.put("6", 20);
		scoreCz.put("7", 30);
		scoreCz.put("8", 40);
		scoreCz.put("9", 20);
		scoreCz.put("10", 40);
		scoreCz.put("11", 60);
		scoreCz.put("12", 50);

		Map<String, Map<String, Object>> cityScores = new HashMap<String, Map<String, Object>>();
		cityScores.put("南京", scoreNj);
		cityScores.put("南通", scoreNt);
		cityScores.put("无锡", scoreWx);
		cityScores.put("镇江", scoreZj);
		cityScores.put("苏州", scoreSz);
		cityScores.put("常州", scoreCz);

		data1 = new ArrayList<Map<String, Map<String, Object>>>();
		data1.add(cityScores);

		/**
		 * 饼状图测试数据
		 */
		pieData = new HashMap<Object, Object>();
		pieData.put("南京", 23);
		pieData.put("南通", 17);
		pieData.put("无锡", 13);
		pieData.put("苏州", 12);
		pieData.put("镇江", 20);
		pieData.put("常州", 15);

		for (int i = 0; i < charts.length; i++) {
			Map<String, String> maps = new HashMap<String, String>();
			maps.put("itemTitle", charts[i]);
			maps.put("itemContent", charts[i] + "......");
			datas.add(maps);
		}

		lv = (ListView) findViewById(R.id.lv);
		SimpleAdapter sa = new SimpleAdapter(this, datas,
				R.layout.chart_listitem, new String[] { "itemTitle",
						"itemContent" }, new int[] { R.id.itemTitle,
						R.id.itemContent });
		lv.setAdapter(sa);
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				@SuppressWarnings("unchecked")
				HashMap<String, String> map = (HashMap<String, String>) lv
						.getItemAtPosition(arg2);

				startChart(map.get("itemTitle"));
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void startChart(String charType) {
		if ("直方图".equals(charType)) {
			Intent intent = hc.createChartIntent(MainActivity.this, ls,
					"得分柱形图", "江苏省华为区设备负荷得分柱形图", "地市", "分数", 100, 0, 10, true,
					Color.BLACK);
			startActivity(intent);
		} else if ("曲线图".equals(charType)) {
			Intent intent = cc.createLineChartIntent(MainActivity.this, data1,
					"江苏省华为区设备负荷年度得分曲线图", "2013 月份", "分数", 0, 100, 20, 12, true,
					Color.BLACK, null, null);
			startActivity(intent);
		} else if ("饼状图".equals(charType)) {
			Intent intent = pc.createPieChatIntent(MainActivity.this, pieData,
					"江苏省华为区设备负荷月度得分饼状图");
			startActivity(intent);
		}
	}

	public HistogramChart getHc() {
		return hc;
	}

	public void setHc(HistogramChart hc) {
		this.hc = hc;
	}

	public List<Map<String, Object>> getLs() {
		return ls;
	}

	public void setLs(List<Map<String, Object>> ls) {
		this.ls = ls;
	}

	public ListView getLv() {
		return lv;
	}

	public void setLv(ListView lv) {
		this.lv = lv;
	}

	public CurveChart getCc() {
		return cc;
	}

	public void setCc(CurveChart cc) {
		this.cc = cc;
	}

	public List<Map<String, Map<String, Object>>> getData1() {
		return data1;
	}

	public void setData1(List<Map<String, Map<String, Object>>> data1) {
		this.data1 = data1;
	}

	public PieChart getPc() {
		return pc;
	}

	public void setPc(PieChart pc) {
		this.pc = pc;
	}

	public Map<Object, Object> getPieData() {
		return pieData;
	}

	public void setPieData(Map<Object, Object> pieData) {
		this.pieData = pieData;
	}

}
